﻿using System;
//------------------------------------------------------------------------------
// ╭─────────────────────────────╮
// │ ╭─╮     ╭─╮              TM │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// │ │ │     │ │                 │                                           ☺  
// │ │ ╰───╮ │ │ ╭─────╮ ╭─────╮ │     Name:Wedn.Net 模版引擎类        ♮ ♪ ♩
// │ │ ╭─╮ │ │ │ │ ╭─╮ │ │ ╭─╮ │ │                                    ♫ ♭      
// │ │ ╰─╯ │ │ │ │ ╰─╯ │ │ ╰─╯ │ │     Author:iceStone               ♬ ♪       
// │ └─────╯ └─╯ ╰─────╯ ╰───╮ │ │     Chinese:汪磊                              
// │                     ┌───╯ │ │                                              
// │                     ╰─────╯ │   ╠═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╬═╣
// ╰─────────────────────────────╯                                              
//------------------------------------------------------------------------------
using System.Collections.Generic;
using System.Configuration;
using System.Web.Mvc;
using Wedn.Net.Common;
using Wedn.Net.TEngine.Configuration;

namespace Wedn.Net.TEngine
{
    /// <summary>
    /// 模版引擎类
    /// </summary>
    public class WednViewEngine : BuildManagerViewEngine
    {
        /// <summary>
        /// 模版引擎构造
        /// </summary>
        public WednViewEngine() : this(null) { }

        /// <summary>
        /// 模版引擎构造
        /// </summary>
        public WednViewEngine(IViewPageActivator viewPageActivator)
            : base(viewPageActivator)
        {
            var section = ConfigurationManager.GetSection("viewEngine") as ViewEngineConfigurationSection;
            if (null == section)
                throw new Exception("还没有配置你的viewEngine规则,请配置你的viewEngine规则");
            AreaViewLocationFormats = section.AreaViewLocationFormats.ToArray();
            AreaMasterLocationFormats = section.AreaMasterLocationFormats.ToArray();
            AreaPartialViewLocationFormats = section.AreaPartialViewLocationFormats.ToArray();
            ViewLocationFormats = section.ViewLocationFormats.ToArray();
            MasterLocationFormats = section.MasterLocationFormats.ToArray();
            PartialViewLocationFormats = section.PartialViewLocationFormats.ToArray();
            //AreaViewLocationFormats = section.AreaViewLocationFormats.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //AreaMasterLocationFormats = section.AreaMasterLocationFormats.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //AreaPartialViewLocationFormats = section.AreaPartialViewLocationFormats.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //ViewLocationFormats = section.ViewLocationFormats.Replace("{theme_root}", Config.ThemeRoot).Replace("{theme_name}", Setting.Theme).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //MasterLocationFormats = section.MasterLocationFormats.Replace("{theme_root}", Config.ThemeRoot).Replace("{theme_name}", Setting.Theme).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            //PartialViewLocationFormats = section.PartialViewLocationFormats.Replace("{theme_root}", Config.ThemeRoot).Replace("{theme_name}", Setting.Theme).Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

            #region 移到配置文件

            //AreaViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            //AreaMasterLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            //AreaPartialViewLocationFormats = new[] { "~/Areas/{2}/Views/{1}/{0}.cshtml", "~/Areas/{2}/Views/{1}/{0}.vbhtml", "~/Areas/{2}/Views/Shared/{0}.cshtml", "~/Areas/{2}/Views/Shared/{0}.vbhtml" };
            //ViewLocationFormats = new[] { Config.ThemeRoot + Setting.Theme + "/Views/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/{0}.vbhtml", Config.ThemeRoot + Setting.Theme + "/Views/{1}/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/{1}/{0}.vbhtml", Config.ThemeRoot + Setting.Theme + "/Views/Shared/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/Shared/{0}.vbhtml" };
            //MasterLocationFormats = new[] { Config.ThemeRoot + Setting.Theme + "/Views/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/{0}.vbhtml", Config.ThemeRoot + Setting.Theme + "/Views/{1}/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/{1}/{0}.vbhtml", Config.ThemeRoot + Setting.Theme + "/Views/Shared/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/Shared/{0}.vbhtml" };
            //PartialViewLocationFormats = new[] { Config.ThemeRoot + Setting.Theme + "/Views/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/{0}.vbhtml", Config.ThemeRoot + Setting.Theme + "/Views/{1}/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/{1}/{0}.vbhtml", Config.ThemeRoot + Setting.Theme + "/Views/Shared/{0}.cshtml", Config.ThemeRoot + Setting.Theme + "/Views/Shared/{0}.vbhtml" };
            //FileExtensions = new[] { "cshtml", "vbhtml" }; 

            #endregion
        }
        /// <summary>
        /// 创建局部视图
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="partialPath"></param>
        /// <returns></returns>
        protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath)
        {
            IEnumerable<string> fileExtensions = FileExtensions;
            return new RazorView(controllerContext, partialPath, null, false, fileExtensions, base.ViewPageActivator);
        }
        /// <summary>
        /// 创建视图
        /// </summary>
        /// <param name="controllerContext"></param>
        /// <param name="viewPath"></param>
        /// <param name="masterPath"></param>
        /// <returns></returns>
        protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath)
        {
            string layoutPath = masterPath;
            IEnumerable<string> fileExtensions = FileExtensions;
            return new RazorView(controllerContext, viewPath, layoutPath, true, fileExtensions, base.ViewPageActivator);
        }
    }
}
